﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

using Telerik.Sitefinity.Configuration;
using Telerik.Sitefinity.Localization.Configuration;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web.Services;

namespace Migration.Classes
{
	public static class MigrationCultures
	{
		private static ConfigManager m_ConfigManager;
		private static CultureInfo m_DefaultCulture;

		static MigrationCultures()
		{
            m_ConfigManager = ConfigManager.GetManager();
		}

		public static CultureInfo DefaultCulture
		{
			get
			{
				if (m_ConfigManager.Provider == null)
				{
					m_ConfigManager = ConfigManager.GetManager();
				}

				if (m_DefaultCulture == null)
				{
					var config = m_ConfigManager.GetSection<ResourcesConfig>();
					m_DefaultCulture = new CultureInfo(config.DefaultCulture.Culture);
				}

				return m_DefaultCulture;
			}
		}

		public static bool IsMultilingual
		{
			get
			{
				if (m_ConfigManager.Provider == null)
				{
					m_ConfigManager = ConfigManager.GetManager();
				}

				var config = m_ConfigManager.GetSection<ResourcesConfig>();

				return config.Multilingual;
			}
		}

        public static bool AllCulturesPresent(List<string> cultures)
        {
            if (m_ConfigManager.Provider == null)
            {
                m_ConfigManager = ConfigManager.GetManager();
            }

            var config = m_ConfigManager.GetSection<ResourcesConfig>();

            foreach (var culture in cultures)
            {
                var cultureInfo = new CultureInfo(culture);
                var found = false;

                foreach (CultureElement configCulture in config.Cultures)
                {
                    if (configCulture.UICulture == cultureInfo.Name)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                    return false;
            }

            return true;
        }
	}
}
